<?php
@session_start();
require_once(dirname(dirname(__FILE__))."/include/init.php");
require_once(dirname(dirname(__FILE__))."/include/mysql.php");
require_once(dirname(dirname(__FILE__))."/include/function.php");
$webconfig = lyg::readArr("web");
$eptime_appid = $webconfig['wx_appid'];//微信公众号APPID
$eptime_secret = $webconfig['wx_secret'];//微信公众号密钥
$eptime_url=$webconfig['wx_url'];//系统安装地址，加http://或者https://
//$eptime_appid = 'wxa628d41e8c7aeb9d';//微信公众号APPID
//$eptime_secret = '15862140d1417e7a7079d6f72b9c108c';//微信公众号密钥
//$eptime_url='http://jz.gyenv.net/m';//系统安装地址，加http://或者https://
$code = $_GET["code"];
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$eptime_appid.'&secret='.$eptime_secret.'&code='.$code.'&grant_type=authorization_code';

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$get_token_url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res,true);

//根据openid和access_token查询用户信息
$access_token = $json_obj['access_token'];
$openid = $json_obj['openid'];



$response2 = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$eptime_appid.'&secret='.$eptime_secret.'';
$ch2 = curl_init();
curl_setopt($ch2,CURLOPT_URL,$response2);
curl_setopt($ch2,CURLOPT_HEADER,0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 10);
$res2 = curl_exec($ch2);
curl_close($ch2);
$json_obj2 = json_decode($res2,true);
$access_token2 = $json_obj2['access_token'];
$errcode = $json_obj2['errcode'];

if($openid == ''){
	header("Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$eptime_appid.'&redirect_uri=".$eptime_url."/oauth2.php&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1&from=timeline&isappinstalled=0#wechat_redirect");
	exit();
}

else{
	
/*
$response = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
$ch1 = curl_init();
curl_setopt($ch1,CURLOPT_URL,$response);
curl_setopt($ch1,CURLOPT_HEADER,0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch1);
curl_close($ch1);
$json_obj1 = json_decode($res,true);
$nickname = $json_obj1['nickname'];
*/



$response1 = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token2.'&openid='.$openid.'&lang=zh_CN';
$ch1 = curl_init();
curl_setopt($ch1,CURLOPT_URL,$response1);
curl_setopt($ch1,CURLOPT_HEADER,0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, 10);
$res1 = curl_exec($ch1);
curl_close($ch1);
//$res1=mb_convert_encoding($res1,'UTF-8','UTF-8,GBK,GB2312,BIG5');
$json_obj1 = json_decode($res1,true);
$subscribe = $json_obj1['subscribe'];
$nickname = $json_obj1['nickname'];
$headimgurl = $json_obj1['headimgurl'];

//$nickname=mb_convert_encoding($nickname,'UTF-8','UTF-8,GBK,GB2312,BIG5');

if ($subscribe !== 1){
	echo "<div style='font:28px; text-align:center;'>未关注公众号！请先关注，谢谢！</div>";
   echo "<script language=\"JavaScript\">window.location.href='https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzIzNjA2NTQ4Nw==#wechat_redirect';\r\n</script>";
	exit;      }


    $_SESSION['headimgurl'] = $headimgurl;
    $_SESSION['nickname'] = $nickname;

	$_SESSION['openid'] = $openid;
	header("Location:".$eptime_url."/index.php");
}

?>